<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                <!--<el-button type="danger" class="butT" @click="delSelected()">删除选中</el-button>-->
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" @selection-change="changeFun" stripe highlight-current-row>
                <el-table-column type="selection" align="center" ></el-table-column>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="菜单访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标码" align="center"></el-table-column>
                <el-table-column prop="description" label="菜单说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="类型" prop="level" >
                                    <el-select v-model="formData.level">
                                        <el-option label="一级菜单" value="1"></el-option>
                                        <el-option label="二级菜单" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="路由路径" prop="path"><!--提示路径书写格式-->
                                    <el-input v-model="formData.path" placeholder="格式: /2-3 " />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" v-if="formData.level === '1'">
                                <el-form-item label="路由路径"><!--提示路径书写格式-->
                                    <el-input v-model="formData.path" placeholder="格式: 1 " />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="父菜单"><!--展示父菜单所有名称-->
                                    <el-select v-model="formData.parentMenuId" placeholder="请选择">
                                        <el-option v-for="p in parents" :key="p.id":label="p.name" :value ="p.path">

                                            <span style="float: left">{{ p.name }}</span>
                                            <span style="float: right; color: #8492a6; font-size: 13px">{{ p.path }}</span>
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="菜单页面名"><!--二级-->
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12" v-if="formData.level === '1'">
                                <el-form-item label="图标码"><!--一级-->
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="类型" prop="level" >
                                    <el-select v-model="formData.level">
                                        <el-option label="一级菜单" value="1" ></el-option>
                                        <el-option label="二级菜单" value="2" ></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="路由路径"><!--提示路径书写格式-->
                                    <el-input v-model="formData.path" placeholder="格式: /2-3 " />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" v-if="formData.level === '1'">
                                <el-form-item label="路由路径"><!--提示路径书写格式-->
                                    <el-input v-model="formData.path" placeholder="格式: 1 " />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="父菜单"><!--展示父菜单所有名称-->
                                    <el-select v-model="parents.name" placeholder="请选择">
                                        <el-option v-for="p in parents" :key="p.id":label="p.name" :value ="p.path">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12" v-if="formData.level === '2'">
                                <el-form-item label="菜单页面名"><!--二级-->
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12" v-if="formData.level === '1'">
                                <el-form-item label="图标码"><!--一级-->
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },

            parents:[], //新增和编辑表单中对应的父菜单列表数据
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据(新增和修改表单)
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }],
                level: [{ required: true, message: '菜单级别为必选项', trigger: 'blur' }],
                path: [{ required: true, pattern : /^\/[1-9]-[1-9]$/,message: '路径格式不正确', trigger: 'blur' }]
            },
            checkBoxData:[],
            checkBoxIds:[]
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();

        },
        watch:{//监听条件变化,一旦变化,将查询第一页
            'pagination.queryString': {
                handler:function(){
                    this.pagination.currentPage = 1;
                }
            }
        },
        methods: {
            //编辑表单
            handleEdit() {//dataEditForm
                this.$refs['dataEditForm'].validate(validate=>{
                    if (validate){
                        axios.post("/menu/edit.do",this.formData).then(res=>{
                            if (res.data.flag){

                                this.findPage();
                                this.$message.success(res.data.message);

                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        this.$message.error("页面参数修改有误!");
                    }

                });
                this.dialogFormVisible4Edit = false;
                this.resetForm();
            },
            //添加
            handleAdd () {
                //表单校验
                this.$refs['dataAddForm'].validate(validate=>{
                    if (validate){
                        //验证通过发送异步请求
                        axios.post("/menu/add.do",this.formData).then(res=>{

                            if (res.data.flag) {

                                this.$message.success(res.data.message);
                                this.findPage();

                            }else {
                                this.$message.error(res.data.message);
                            }
                        });

                    } else {
                        this.$message.error("页面参数有误!")
                    }
                });
                this.dialogFormVisible = false;


            },
            //分页查询

            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };

                //发送ajax请求，进行分页查询
                axios.post("/menu/findAll.do", param).then((res) => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                });

            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;//添加弹框显示
                this.resetForm();//保证之前的未提交的表单数据以及编辑回显的数据在add操作是表单为空
                axios.get("/menu/findParentmenu.do").then((res) => {
                    if(res.data.flag){
                        this.parents = res.data.data;
                    }else{
                        this.$message.error(res.data.message);
                    }

                })
            },
            // 弹出编辑窗口    并且回显数据
            handleUpdate(row) {

                axios.get("/menu/findByMenuId.do?id="+row.id).then(
                    (res)=>{
                        this.formData = res.data.data;//回显数据时,已经将id信息放入formData中
                        if(this.formData.parentMenuId != null){
                            axios.get("/menu/findByMenuId.do?id="+this.formData.parentMenuId).then((res)=>{
                                if(res.data.flag){
                                    this.parents = res.data.data;
                                }else{
                                    this.$message.error(res.data.message);
                                }
                            })
                        }

                    }
                );
                this.dialogFormVisible4Edit = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//修改当前页码

                this.findPage();//调用查询
            },
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定删除将此记录永久删除吗?","tips",{
                    type:"warning"
                }).then(()=>{//点击确认按钮执行

                    axios.get("/menu/delete.do?id="+row.id).then(res=> {
                        if (res.data.flag) {

                            this.$message.success(res.data.message);
                            this.findPage();

                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).catch(r=>{
                        //失败的响应,没有相应的权限删除
                        console.log(r);
                        this.showMessage(r);

                    });

                }).catch(()=>{
                    this.$message.info("操作已取消")
                });
            },
            //复选框选中数据
            changeFun(val){
                //alert(JSON.stringify(val));
                this.checkBoxData = val;

            },
            /*//逻辑删除选中
            delSelected(){
                this.$confirm("确定删除选中项吗","tips",{type:"warning"}).then(()=>{

                    axios.post("/menu/delSelected",this.checkBoxData).then(res=>{

                        if (res.data.flag) {

                            this.$message.success(res.data.message);
                            this.findPage();
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });
                }).catch(()=>{
                    this.$message.info("已取消操作")
                });
            }*/
        }
    })
</script>
</html>






































